module.exports = app => {
    const { STRING, INTEGER, DATE } = app.Sequelize;
    const User = app.model.define('user', {
        userId: { type: INTEGER, primaryKey: true, autoIncrement: true },
        token: STRING,
        username: STRING(30),
        avatar: STRING,
        name: STRING(30),
        mobile: STRING(11),
        eMail: STRING,
        password: STRING(32),
        gender: INTEGER,
        age: INTEGER,
        state: INTEGER,
        last_sign_in_at: DATE,
        created_at: DATE,
        updated_at: DATE,
    });



    User.findLogin = async function (username, password) {
        return await this.findOne({
            where: {
                username: username,
                password
            }
        });

    }
    User.findByToken = async function (token) {
        return await this.findOne({
            attributes: ['userId', 'username','avatar'],
            where: {
                token
            }
        });

    }



    // don't use arraw function
    User.prototype.logSignin = async function () {
        return await this.update({ last_sign_in_at: new Date() });
    }
    // don't use arraw function
    User.prototype.updateToken = async function (token) {
        return await this.update({ token});
    }

    return User;
};